<!DOCTYPE html>
            
<HTML>
<HEAD>
<meta name="booktitle" content="Developing Applications With Objective Caml" >
 <meta charset="ISO-8859-1"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<META name="GENERATOR" content="hevea 1.05-7 of 2000-02-24">
<META NAME="Author" CONTENT="Christian.Queinnec@lip6.fr">
<LINK rel=stylesheet type="text/css" href="videoc-ocda.css">
<script language="JavaScript" src="videoc.js"><!--
//--></script>
<TITLE>
 Introduction
</TITLE>
</HEAD>
<BODY class="regularBody">
<A HREF="book-ora110.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora113.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2> Introduction</H2>
<A NAME="@concepts252"></A>
<A NAME="@concepts253"></A>
Developing programs in a given language very often requires one to
integrate libraries written in other languages. The two main reasons
for this are:
<UL>
<LI>
 to use libraries that cannot be written in the language, thus
extending its functionality;

<LI> to use high-performance libraries already implemented in another
language.
</UL>
A program then becomes an assembly of software components written in
various languages, where each component has been written in the
language most appropriate for the part of the problem it addresses.
Those software components interoperate by exchanging values and
requesting computations.<BR>
<BR>
The Objective CAML language offers such a mechanism for interoperability with
the C language. This mechanism allows Objective CAML code to call C
functions with Caml-provided arguments, and to get back the result of
the computation in Objective CAML. The converse is also possible: a C
program can trigger an Objective CAML computation, then work on its result.<BR>
<BR>
The choice of C as interoperability language is justified by the
following reasons:
<UL>
<LI>
 it is a standardized language (ISO C);

<LI> C is a popular implementation language for operating systems
(Unix, Windows, MacOS, etc.); 

<LI> a great many libraries are written in C;

<LI> most programming languages offer a C interface, thus it is
possible to interface Objective CAML with these languages by going through C.
</UL>
The C language can therefore be viewed as the esperanto of programming
languages.<BR>
<BR>
Cooperation between C and Objective CAML raises a number of difficulties
that we review below.
<UL>
<LI>
 <B>Machine representation of data</B><BR>For instance, values of base types (<I>int</I>, <I>char</I>,
<I>float</I>) have different machine representations in the two
languages. This requires conversion between the representations, in
both directions. The same holds for data structures such as records,
sum types<A NAME="text28" HREF="book-ora122.html#note28"><SUP><FONT SIZE=2>1</FONT></SUP></A>, or arrays.<BR>
<BR>

<LI> <B>The </B><B>Objective CAML</B><B> garbage collector</B><BR>Standard C does not provide garbage collection. (However, garbage
collectors are easily written in C.) Moreover, calling a C function
from Objective CAML must not modify the memory in ways incompatible with the
Objective CAML GC.

<LI> <B>Aborted computations</B><BR>Standard C does not support exceptions, and provides different
mechanisms for aborting computations. This complicates Objective CAML's
exception handling.

<LI> <B>Sharing common resources</B><BR>For instance, files and other input-output devices are shared between
Objective CAML and C, but each language maintains its own input-output buffers.
This may violate the proper sequencing of input-output operations in
mixed programs.
</UL>
Programs written in Objective CAML benefit from the safety of static typing
and automatic memory management. This safety must not be compromised
by improper use of C libraries and interfacing with other languages
through C. The programmer must therefore adhere to rather strict
rules to ensure that both languages coexist peacefully.<BR>
<BR>
<HR>
<A HREF="book-ora110.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora113.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
